package nyist.media.controller;

import nyist.media.annotation.Log;
import nyist.media.bean.admin.AdminInfoBean;
import nyist.media.entity.admin.Admin;
import nyist.media.page.ApiResult;
import nyist.media.service.AdminService;
import nyist.media.service.RoleService;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 管理员管理
 *
 * @author : dcf
 * @date : 2019/3/12
 */
@RequestMapping(value = "/admin/users", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
public class AdminController extends BaseController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private RoleService roleService;

    /**
     * 个人资料管理，查看，编辑修改
     *
     * @param edit 是否编辑
     * @return 返回数据信息
     */
    @GetMapping("info")
    public Map<String, Object> info(Boolean edit) {
        Map<String, Object> map = new HashMap<>(2);
        if (edit != null && edit) {
            map.put("result", adminService.getCurrent());
        }
        map.put("result", new AdminInfoBean(adminService.getCurrent()));
        return map;
    }

    /**
     * 分页查询管理员信息
     *
     * @param keyword 搜索关键字
     * @param page    页码
     * @param limit   页长
     * @return 分页列表信息
     */
    @GetMapping
    public Map<String, Object> list(String keyword, Integer page, Integer limit) {
        PageRequest pageRequest = new PageRequest(page - 1, limit);
        return toResultPage(adminService.findAdminList(pageRequest, keyword));
    }

    @Log(value = "查看管理员信息")
    @GetMapping("{id:\\d+}")
    public Map<String, Object> view(@PathVariable Long id, Boolean edit) {
        if (edit != null && edit) {
            return ApiResult.SUCCESS.getMap().add("roleList", roleService.findAll());
        }
        return ApiResult.SUCCESS.getMap().add("result", adminService.getInfo(id));
    }

    @Log("更新管理员")
    @PutMapping("{id:\\d+}")
    public Map<String, Object> update(@PathVariable Long id, @RequestBody AdminInfoBean param) {
        param.setId(id);
        adminService.update(param);
        return ApiResult.SUCCESS.getMap();
    }

    @Log("更新个人信息")
    @PutMapping
    public Map<String, Object> updateCurrent(@RequestBody AdminInfoBean param) {
        adminService.updateCurrent(param);
        return ApiResult.SUCCESS.getMap();
    }

    @Log("新增管理员")
    @PostMapping
    public Map<String, Object> save(AdminInfoBean param) {
        adminService.save(param);
        return ApiResult.SUCCESS.getMap();
    }

    /**
     * 校驗用戶是否存在
     *
     * @param username 用戶名
     * @return true 存在，false不存在
     */
    @GetMapping("validate/{username}")
    public Map<String, Object> validateUsername(@PathVariable String username) {
        return ApiResult.SUCCESS.getMap().add("exist", adminService.findByUsername(username) != null);
    }

    @Log("删除管理员")
    @DeleteMapping("{id:\\d+}")
    public Map delete(@PathVariable Long id) {
        adminService.delete(id);
        return ApiResult.SUCCESS.getMap();
    }

    /**
     * 密码校验
     *
     * @param pwd 密码
     * @return 结果信息
     */
    @GetMapping("validate_pwd")
    public Map<String, Object> validatePwd(String pwd) {
        Admin admin = adminService.getCurrent();
        if (admin.getPassword().equalsIgnoreCase(DigestUtils.md5Hex(pwd.getBytes()))) {
            return ApiResult.SUCCESS.getMap();
        }
        return ApiResult.FAIL.getMap("密码校验失败");
    }

    /**
     * 管理员切换启用禁用功能
     *
     * @param id 管理员ID
     * @return 成功标志
     */
    @PutMapping("/{id}/toggle_enabled")
    public Map<String, Object> swichAdmin(@PathVariable Long id) {
        return ApiResult.SUCCESS.getMap().add("enabled", adminService.toggleEnabled(id).isEnabled());
    }

    @PutMapping("/audit/{applyId}")
    public Map<String, Object> auditApplyOrders(@PathVariable Long applyId) {

        return ApiResult.SUCCESS.getMap();
    }


}